package com.check.operation.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.CheckApplication;
import com.check.operation.R;
import com.check.operation.model.Operation;
import com.check.operation.model.Result;
import com.check.operation.model.SpecimenInfo;
import com.check.operation.service.CheckAdapter;
import com.check.operation.service.ShAdapter;
import com.check.operation.util.ThreadLocalDateUtil;
import com.szj.basic.view.BaseActivity2;
import com.szj.util.HttpUtil;
import com.szj.util.SysUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

@EActivity(R.layout.activity_check)
public class CheckActivity extends BaseActivity2 implements View.OnClickListener {

    public static final Integer CHECK_RESULT_CODE = 100;

    @ViewById
    ListView listview_bbsy;
    @ViewById
    Button btn_sure;
    @ViewById
    Button btn_cancel;
    @ViewById
    TextView sqdh;
    @ViewById
    TextView ssjh;
    @ViewById
    TextView bedno;
    @ViewById
    TextView zyh;
    @ViewById
    TextView xm;
    @ViewById
    TextView djsj;

    CheckAdapter checkAdapter;

    Bundle data;

    boolean checked = false;

    @AfterViews
    public void initView() {
        data = getIntent().getBundleExtra("data");
        btn_sure.setOnClickListener(this);

        checkAdapter = new CheckAdapter(getLayoutInflater(), new ArrayList<String>());
        listview_bbsy.setAdapter(checkAdapter);
    }

    @Override
    public void onGetBarValue(String barcode) {
        String inpat = data.getString("inpat");
        String action = new StringBuilder("bbsy!check.action?inpat=").append(inpat)
                .append("&barcode=").append(barcode).toString();
        String rs = HttpUtil.get("http://199.199.4.20:8085/rmyyblksqd/", action);
        if(SysUtil.isNotNUll(rs)) {
            Result<SpecimenInfo> result = JSONObject.parseObject(rs,  new TypeReference<Result<SpecimenInfo>>() {
            });
            if("success".equals(result.code)) {
                setInfo(result.value);
                checkAdapter.setData(result.value.getBqsms());
                checkAdapter.notifyDataSetChanged();
                checked = true;
                showInfo(result.text);
            } else if("error".equals(result.code)) {
                showInfo(result.text);
                checked = false;
            }
        }
    }

    @UiThread
    public void showInfo(String text) {
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }

    @UiThread
    public void setInfo(SpecimenInfo info ) {
        sqdh.setText(info.getSqdh());
        ssjh.setText(info.getSsjh());
        xm.setText(info.getBrxm());
        zyh.setText(info.getZyh());
        djsj.setText(ThreadLocalDateUtil.formatDate(info.getCjsmsj(), "yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public void onGetRfidValue(String arg0) {}

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Intent intent = new Intent();
        switch (id) {
            case R.id.btn_sure:
                data.putBoolean("checked", checked);
                intent.putExtra("data", data);
                setResult(MainActivity.CHECK_RESULT_CODE, intent);
                finish();
                break;
            case R.id.btn_cancel:
                data.putBoolean("checked", false);
                intent.putExtra("data", data);
                setResult(MainActivity.CHECK_RESULT_CODE, intent);
                finish();
            default:
                break;
        }
    }

}
